package com.yq.ylmall.product.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yq.ylmall.common.log.annotation.Log;
import com.yq.ylmall.common.utils.AuthContextUtil;
import com.yq.ylmall.dto.product.ProductSpecQueryDto;
import com.yq.ylmall.model.product.ProductSpec;
import com.yq.ylmall.product.service.IProductSpecService;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.result.ResultCode;
import com.yq.ylmall.vo.product.ProductSpecPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品规格 后台前端控制器
 * </p>
 *
 * @author yzk789
 * @since 2024-05-30
 */
@Api(tags = "商品规格管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/product/spec")
@Slf4j
public class ProductSpecAdminController {

    private final IProductSpecService productSpecService;

    @ApiOperation(value = "分页查询商品规格")
    @PostMapping("/page")
    public Result<IPage<ProductSpecPageVo>> page(@RequestBody ProductSpecQueryDto dto) {
        log.info("用户名{}", AuthContextUtil.get().getUsername());
        return Result.ok(productSpecService.queryProductSpec(dto));
    }

    @ApiOperation(value = "根据ID查询商品规格所有信息")
    @GetMapping("/get/{id}")
    public Result<ProductSpecPageVo> getProductSpecInfoById(@PathVariable Long id) {
        return Result.ok(productSpecService.getProductSpecInfoById(id));
    }

    //    @Log(title = "商品规格:全部查询", businessType = 0)
    @ApiOperation(value = "获取全部商品规格")
    @GetMapping("/get/all")
    public Result<List<ProductSpecPageVo>> findAllProductSpec() {
        List<ProductSpecPageVo> list = productSpecService.list().stream()
                .map(spec -> BeanUtil.copyProperties(spec, ProductSpecPageVo.class))
                .collect(Collectors.toList());
        return Result.ok(list);
    }

    @Log(title = "商品规格:名称查询", businessType = 0)
    @ApiOperation(value = "根据商品规格名称查询信息")
    @GetMapping("/get")
    public Result<IPage<ProductSpecPageVo>> getProductSpecInfoByName(@RequestParam String specName) {
        return Result.ok(productSpecService.getProductSpecInfoByName(specName));
    }

    @ApiOperation(value = "新增商品规格")
    @PostMapping("/add")
    public Result<Object> saveProductSpec(@RequestBody ProductSpec dto) {
        productSpecService.addProductSpec(dto);
        return Result.ok(ResultCode.SAVE_SUCCESS);
    }


    @ApiOperation(value = "删除商品规格")
    @PostMapping("/remove/{id}")
    public Result<Object> removeProductSpec(@PathVariable Long id) {
        productSpecService.removeProductSpec(id);
        return Result.ok(ResultCode.DELETE_SUCCESS);
    }

    @ApiOperation(value = "根据ID修改商品规格信息")
    @PutMapping("/update")
    public Result<Object> updateProductSpecById(@RequestBody ProductSpec dto) {
        productSpecService.updateProductSpecById(dto);
        return Result.ok(ResultCode.UPDATE_SUCCESS);
    }

}
